package org.glorylight.hacks.social.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.Type;

@Entity
@Table(name = "scrap_book")
@NamedQueries({
	@NamedQuery(name = "books-by-user", query = "from ScrapBook a where a.userId = :userId"),
	@NamedQuery(name = "books-by-user-date", query = "from ScrapBook a where a.userId = :userId " +
				"AND (a.dateCreated BETWEEN :startDate AND :endDate)"),
	@NamedQuery(name = "books-by-user-feed", query = "from ScrapBook a where a.userId in(:userIds) " +
			"order by a.dateCreated"),
	@NamedQuery(name = "books-by-date", query = "from ScrapBook a where a.dateCreated BETWEEN :startDate AND :endDate")
	})
public class ScrapBook implements Serializable {
	
	private static final long serialVersionUID = -1876229959660699691L;

	@Id
	@Type(type = "uuid")
	@Column(length = 32)
	private UUID id;

	@Column(name = "userId")
	private String userId;//this is email

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date_created")
	private Date dateCreated;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date_updated")
	private Date dateUpdated;

	@Column(name = "pic_path")
	private String picPath;
	
	@Column(name = "title")
	private String title;
	
	@Column(name = "description")
	private String description;
	
	@Column(name = "is_shared")
	private boolean isShared;

	@OneToMany(mappedBy = "scrapBook", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	private List<ScrapBookVotes> voters = new ArrayList<ScrapBookVotes>();
	
	@OneToMany(mappedBy = "scrapBook", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	private List<ScrapBookComments> comments = new ArrayList<ScrapBookComments>();
	
	@ManyToOne
	@JoinColumn(name = "tag_name")
	private Tag tag;
	
	public ScrapBook(UUID id, String userId, Date dateCreated, String picPath
			, String title, String description, boolean isShared) {
		this.dateCreated = dateCreated;
		this.picPath = picPath;
		this.title = title;
		this.isShared = isShared;
		this.userId = userId;
		this.id = id;
		this.description = description;
	}
	
	public ScrapBook(){
	}
	
	public UUID getId() {
		return id;
	}
	
	public void setId(UUID id) {
		this.id = id;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public Date getDateCreated() {
		return dateCreated;
	}

	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}

	public Date getDateUpdated() {
		return dateUpdated;
	}

	public void setDateUpdated(Date dateUpdated) {
		this.dateUpdated = dateUpdated;
	}

	public String getPicPath() {
		return picPath;
	}

	public void setPicPath(String picPath) {
		this.picPath = picPath;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public boolean isShared() {
		return isShared;
	}

	public void setShared(boolean isShared) {
		this.isShared = isShared;
	}

	public Tag getTag() {
		return tag;
	}

	public void setTag(Tag tag) {
		this.tag = tag;
	}
	
	@Transient
	public String getTagName() {
		if(tag != null)
			return tag.getTagName();
		return null;
	}

	public List<ScrapBookVotes> getVoters() {
		return voters;
	}

	public void setVoters(List<ScrapBookVotes> voters) {
		this.voters = voters;
	}

	public List<ScrapBookComments> getComments() {
		return comments;
	}

	public void setComments(List<ScrapBookComments> comments) {
		this.comments = comments;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ScrapBook other = (ScrapBook) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	
}
